﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MebAppLib
{
    /// <summary>
    /// 商店应用运行时
    /// </summary>
    public class AppRuntime
    {
        public string UserID { get; set; }

        public DateTime StartupTime { get; set; }

        internal AppRuntime()
        {

        }
        static bool _initialized;
        static AppRuntime _current;
        public static AppRuntime Current
        {
            get
            {
                if (!_initialized)
                {
                    throw new Exception("请先使用Initialize方法初始化！");
                }
                return _current;
            }
        }
        /// <summary>
        /// 启动路径
        /// </summary>
        public string StartupPath { get; set; }

        /// <summary>
        /// 启动文件
        /// </summary>
        public string StartupFile { get; set; }

        /// <summary>
        /// 启动配置文件（.config）
        /// </summary>
        public string StartupConfigFile { get; set; }

        /// <summary>
        /// 启动文件
        /// </summary>
        public string StartupAssembly { get; set; }

        /// <summary>
        /// 启动类
        /// </summary>
        public string StartupType { get; set; }

        /// <summary>
        /// 启动类MDToken
        /// </summary>
        public int StartupTypeMetadataToken { get; set; }

        /// <summary>
        /// 启动参数
        /// </summary>
        public string StartupArguments { get; set; }


        public string AppID { get; set; }

        public string AppName { get; set; }

        public int AppVersion { get; set; }

        /// <summary>
        /// 用户会话，用于客户端报告事件和鉴权
        /// </summary>
        public string SessionID { get; set; }
        public string InstanceID { get; set; }

        /// <summary>
        /// 在商店应用程序入口调用此方法来初始化AppRuntime
        /// </summary>
        /// <param name="appId"></param>
        public static void Initialize(string appId)
        {
            Guid appGuid;
            if (string.IsNullOrEmpty(appId))
            {
                MessageBox.Show("appId不可以为空！");
                Environment.Exit(520);
            }
            else if (!Guid.TryParse(appId, out appGuid))
            {
                MessageBox.Show("appId格式不正确！");
                Environment.Exit(521);
            }
            else
            {
                //if (Environment.MachineName == "DESKTOP-FQM3FGO")
                //{
                //    return;
                //}
                try
                {
                    _current = SharedMemroy.GetData<AppRuntime>(appId);
                    _initialized = true;
                }
                catch (FileNotFoundException ex)
                {
                    MessageBox.Show("只能从应用启动器中打开！");
                    //throw new Exception("商店应用程序只能从应用启动器中启动！");
                    Environment.Exit(620);
                }
            }
        }
    }
}
